/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "It_process_plimits.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include <string>
#include "stdio.h"
#include <climits>
#include <climits>
#include <sys/types.h>
#include <gtest/gtest.h>
#include <dirent.h>
#include <sys/stat.h>
#include <regex>
#include <iostream>
#include <fstream>

using namespace std;

static UINT32 Testcase(VOID)
{
    const size_t BUFFER_SIZE = 512;
    int ret, matchResult;
    char buf[BUFFER_SIZE + 1] = {0};
    char *groupStr = "/proc/plimits/test013";
    char plimitDir[TEST_BUFFER_SIZE];
    char plimitQuotaFile[TEST_BUFFER_SIZE];
    char plimitPeriodFile[TEST_BUFFER_SIZE];
    char plimitStatFile[TEST_BUFFER_SIZE];

    sprintf(plimitStatFile, "%s/sched.stat", groupStr);
    sprintf(plimitQuotaFile, "%s/sched.quota", groupStr);
    sprintf(plimitPeriodFile, "%s/sched.period", groupStr);

    mode_t mode;
    ret = mkdir(groupStr, S_IFDIR | mode);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);

    std::ifstream infile;
    infile.open(plimitStatFile);
    string line;
    while(!infile.eof()) {
        getline(infile, line);
        regex e(R"(\ballRuntime\b)");
        matchResult = regex_search(line, e);
        if (matchResult == 1) {
            break;
        }
    }
    infile.close();
    ICUNIT_ASSERT_EQUAL(matchResult, 1, matchResult);

    memset(buf, 0, BUFFER_SIZE);
    ret = ReadFile(plimitQuotaFile, buf);
    matchResult = std::regex_match(buf, std::regex("\\d+\n+"));
    ICUNIT_ASSERT_EQUAL(matchResult, 1, matchResult);

    memset(buf, 0, BUFFER_SIZE);
    ret = ReadFile(plimitPeriodFile, buf);
    matchResult = std::regex_match(buf, std::regex("\\d+\n+"));
    ICUNIT_ASSERT_EQUAL(matchResult, 1, matchResult);

    return 0;
}

VOID ItProcessPlimitsSched013(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_SCHED_013", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}

